package com.gitee.webdav.logger;

import com.gitee.webdav.kernel.Remark;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.ILoggerFactory;
import org.slf4j.event.Level;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 定制化日志工厂
 *
 * @author hongda.li
 */
public final class LoggerFactory implements ILoggerFactory {

    @Remark("日志级别")
    @Getter
    @Setter
    private static Level level = Level.DEBUG;

    @Remark("日志黑名单")
    private static final Set<String> DISABLED = new HashSet<>();

    /**
     * Logger 缓存
     * 避免反复多次创建重复的 Logger
     */
    private final Map<String, Logger> loggerCache = new ConcurrentHashMap<>(2 << 5);

    static {
        new Logger(LoggerFactory.class.getName()).info("Logger Level: {}", level);
    }

    public static void disabled(Class<?> clazz) {
        disabled(clazz.getName());
    }

    public static void disabled(String name) {
        DISABLED.add(name);
    }

    public static boolean isDisabled(String name) {
        return DISABLED.contains(name);
    }

    @Override
    public org.slf4j.Logger getLogger(String name) {
        return this.loggerCache.computeIfAbsent(name, Logger::new);
    }
}
